//
//  SList.c
//  链表(C)
//
//  Created by Yan Zihao on 2024/12/15.
//
#include "SList.h"

SLTNode* SLTBuyNode(SLTDataType x)
{
    SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
    if(newnode == NULL)
    {
        perror("malloc fail:");
        exit(0);
    }
    newnode->data = x;
    newnode->next = NULL;
    return newnode;
}

void SLTPrint(SLTNode* phead)
{
    while(phead)
    {
        printf("%d ",phead->data);
        phead = phead->next;
    }
    printf("NULL\n");
}

//尾插
void SLTPushBack(SLTNode** pphead, SLTDataType x)
{
    assert(pphead);
    SLTNode* newnode = SLTBuyNode(x);
    if(*pphead == NULL)
    {
        *pphead = newnode;
    }
    else
    {
        SLTNode* ptail = *pphead;
        while(ptail->next)
        {
            ptail = ptail->next;
        }
        ptail->next = newnode;
    }
}
//头插
void SLTPushFront(SLTNode** pphead, SLTDataType x)
{
    assert(pphead);
    SLTNode* newnode = SLTBuyNode(x);
    newnode->next = *pphead;
    *pphead = newnode;
}
//尾删
void SLTPopBack(SLTNode** pphead)
{
    assert(pphead && *pphead);
    SLTNode* prev = *pphead;
    SLTNode* ptail = *pphead;
    if(ptail->next == NULL)
    {
        free(ptail);
        *pphead = NULL;
    }
    else
    {
        while(ptail->next)
        {
            prev = ptail;
            ptail = ptail->next;
        }
        free(ptail);
        prev->next = NULL;
    }
}
//头删
void SLTPopFront(SLTNode** pphead)
{
    assert(pphead && *pphead);
    SLTNode* next = (*pphead)->next;
    free(*pphead);
    *pphead = next;
}

//查找
SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
{
    SLTNode* pcur = phead;
    while(pcur)
    {
        if(pcur->data == x)
        {
            return pcur;
        }
        pcur = pcur->next;
    }
    return NULL;
}

//在指定位置之前插入数据
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
    assert(pphead && *pphead);
    assert(pos);
    SLTNode* newnode = SLTBuyNode(x);
    if(pos == *pphead)
    {
        SLTPushFront(pphead, x);
    }
    else
    {
        SLTNode* prev  = *pphead;
        while(prev->next != pos)
        {
            prev = prev->next;
        }
        newnode->next = pos;
        prev->next = newnode;
    }
}
//在指定位置之后插入数据
void SLTInsertAfter(SLTNode* pos, SLTDataType x)
{
    assert(pos && pos->next);
    SLTNode* newnode = SLTBuyNode(x);
    newnode->next = pos->next;
    pos->next = newnode;
}

//删除pos节点
void SLTErase(SLTNode** pphead, SLTNode* pos)
{
    assert(pphead && *pphead);
    assert(pos);

    if(*pphead == pos)
    {
        SLTPopFront(pphead);
    }
    else
    {
        SLTNode* prev = *pphead;
        while(prev->next != pos)
        {
            prev = prev->next;
        }
        prev->next = pos->next;
        free(pos);
        pos = NULL;
    }

}
//删除pos之后的节点
void SLTEraseAfter(SLTNode* pos)
{
    assert(pos && pos->next);
    SLTNode* del = pos->next;
    pos->next = del->next;
    free(del);
    del = NULL;
}

//销毁链表
void SListDesTroy(SLTNode** pphead)
{
    assert(pphead && *pphead);
    SLTNode* pcur = *pphead;
    while(pcur)
    {
        SLTNode* next = pcur->next;
        free(pcur);
        pcur = next;
    }
    *pphead = NULL;
}
